package com.example.meizu002.controller;

import com.example.meizu002.entity.Goods1;
import com.example.meizu002.service.Goods1Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("Goods1")
@CrossOrigin
@Slf4j
public class Goods1Controller {
    @Autowired
    private Goods1Service goods1Service;
   @GetMapping("/findAll")
    public Map<String,Object> findAll(){
        Map<String,Object> map=new HashMap<>();
        try {
            List<Goods1> goods1s=goods1Service.findAll();
            map.put("records",goods1s);
            map.put("state",true);
            map.put("code", 200);
            map.put("msg", "商品信息查询成功!");
        }catch (Exception e){
            e.printStackTrace();
            map.put("state", false);
            map.put("code", 500);
            map.put("msg", "商品信息查询失败!");
        }
        return map;
    }
    @GetMapping("/deleteGoods")
    public Map<String,Object> deleteGoods(int id){
       log.info("删除商品的id:[{}]",id);
       Map<String,Object> map=new HashMap<>();
       try {
            goods1Service.deleteGoods(id);
           map.put("state",true);
           map.put("code", 200);
           map.put("msg","删除商品信息成功!");
       }catch (Exception e){
           e.printStackTrace();
           map.put("state",false);
           map.put("code", 500);
           map.put("msg","删除商品信息失败!");
       }
       return map;
    }
    @GetMapping("/findOne")
    public Map<String,Object> findOne(int id){
        Map<String,Object> map=new HashMap<>();
        try {
            Goods1 goods1=goods1Service.findOne(id);
            map.put("records",goods1);
            map.put("state",true);
            map.put("code", 200);
            map.put("msg", "商品信息查询成功!");
        }catch (Exception e){
            e.printStackTrace();
            map.put("state", false);
            map.put("code", 500);
            map.put("msg", "商品信息查询失败!");
        }
        return map;
    }
    @PostMapping("/addGoods")
    public Map<String,Object>addGoods(Goods1 goods1){
        Map<String, Object> map = new HashMap<>();
        try {
            goods1Service.addGoods(goods1);
            map.put("state",true);
            map.put("code", 200);
            map.put("msg", "商品信息添加成功!");
        }catch (Exception e){
            e.printStackTrace();
            map.put("state", false);
            map.put("code", 500);
            map.put("msg", "商品信息添加失败!");
        }
        return map;
    }
   @PostMapping("/updateGoods")
    public Map<String,Object> updateGoods(Goods1 goods1){
        Map<String, Object> map = new HashMap<>();
        try {
            goods1Service.updateGoods(goods1);
            map.put("state",true);
            map.put("code", 200);
            map.put("msg", "商品信息修改成功!");
        }catch (Exception e){
            e.printStackTrace();
            map.put("state", false);
            map.put("code", 500);
            map.put("msg", "商品信息修改失败!");
        }
        return map;
    }
}
